<template>
  <div class="app-container">
    <el-form ref="form" :model="form" label-width="120px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="采样间隔" prop="SampleInterval">
            <el-select v-model="form.SampleInterval" placeholder="请选择采样间隔" @change="setRandom">
              <el-option label="2分钟" key="120" value="120"/>
              <el-option label="5分钟" key="300" value="300"/>
              <el-option label="10分钟" key="600" value="600"/>
              <el-option label="30分钟" key="1800" value="1800"/>
              <el-option label="1小时" key="3600" value="3600"/>
              <el-option label="2小时" key="7200" value="7200"/>
              <el-option label="4小时" key="14400" value="14400"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="上传包数" prop="UploadFilesCount">
            <el-select v-model="form.UploadFilesCount" placeholder="请选择上传包数">
              <el-option label="1包" key="1" value="1"/>
              <el-option label="2包" key="2" value="2"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="初次采样时刻"><span>{{ form.SynTime|secondToDate }}</span></el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="bottom_wrap">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </div>
</template>
<script>
import {getEquipConfigByCode, updateEquipConfig} from "@/api/equip";

export  default {
  name: "SetEquip",
  props: ["equipmentCode"],
  data(){
    return {
      equip: {
        equipmentCode: "",
        config: ""
      },
      form: {},
      rules: {
        SampleInterval: [
          {required: true, message: "请选择采样间隔", trigger: "change"}
        ],
        UploadFilesCount: [
          {required: true, message: "请选择上传包数", trigger: "change"}
        ]
      }
    }
  },
  watch: {
    equipmentCode() {
      this.$nextTick(() => {
        this.equip.equipmentCode = this.equipmentCode;
        this.reset();
        this.getEquipConfig();
      })
    },
  },
  filters: {
    //秒转换成时分秒格式
    secondToDate: function (value) {
      if (!value) return "";
      let result = parseInt(value);
      let h = Math.floor(result / 3600) < 10 ? "0" + Math.floor(result / 3600) : Math.floor(result / 3600);
      let m = Math.floor((result / 60) % 60) < 10 ? "0" + Math.floor((result / 60) % 60) : Math.floor((result / 60) % 60);
      let s = Math.floor(result % 60) < 10 ? "0" + Math.floor(result % 60) : Math.floor(result % 60);
      let res = "";
      if (h !== "00") res += `${h}时`;
      if (m !== "00") res += `${m}分`;
      res += `${s}秒`;
      return res;
    }
  },
  created() {
    if (this.equipmentCode) {
      this.equip.equipmentCode = this.equipmentCode;
    }
    this.reset();
    this.getEquipConfig();
  },
  methods:{
    getEquipConfig() {
      getEquipConfigByCode(this.equip.equipmentCode).then(res => {
        if (res.code === 200 && res.data) {
          let config = JSON.parse(res.data.config);
          const value = config.iphm_sensor_equipment;
          const type = Object.prototype.toString.call(value);
          if (type === '[object String]') {
            this.form = JSON.parse(value);
          } else if (type === '[object Object]') {
            this.form = value;
          }
        }
      })
    },
    // 表单重置
    reset() {
      this.form = {
        SampleInterval: "300",
        UploadFilesCount: "1",
        SynTime: undefined,
      };
      this.resetForm("form");
    },
    // 根据采样间隔计算每天初次采样时刻(在采样间隔基础上, 先减掉45秒, 然后乘以0~1的随机数)
    setRandom(paramValue) {
      if (!paramValue) return 0;
      let random = Math.random();
      let diff = random * (paramValue - 45);
      if (diff <= 0) diff = 1;
      this.form.SynTime = parseInt(diff);
      return diff;
    },
    /** 关闭配置 */
    cancel() {
      this.$emit('cancel');
    },
    /** 配置提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (!this.form.SynTime) {
            this.setRandom(this.form.SampleInterval);
          }
          // 边缘端 1启用 2采集数据
          this.form.StartUseFlag = "1";
          let config = {
            iphm_sensor_equipment: this.form
          }
          this.equip.config = JSON.stringify(config);
          updateEquipConfig(this.equip).then(res => {
            if (res.code === 200) {
              this.$modal.msgSuccess("保存成功");
              this.cancel();
            }
          })
        }
      })
    }
  }
}
</script>
<style scoped>
.bottom_wrap {
  margin-top: 10px;
  text-align: right;
}
</style>